/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chess.annotation.demo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class ZoomRelativeLayoutV3 extends RelativeLayout implements OnTouchListener {
	private static final int INVALID_POINTER_ID = -1;
	protected State mode = State.NONE;
	private float mPosX;
	private float mPosY;

	private float mLastTouchX;
	private float mLastTouchY;
	private int mActivePointerId = INVALID_POINTER_ID;

	private float mScaleFactor = 1.0f;
	private float miniScaleFactor = 1.0f;
	private float maxScaleFactor = 3.0f;
	protected float disLastTouch = 0;
	protected float touchSlop;
	private Context context;
	private float scaleLastTouch = 1.0f;

	public ZoomRelativeLayoutV3(Context context) {
		this(context, null, 0);
	}

	public ZoomRelativeLayoutV3(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ZoomRelativeLayoutV3(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		this.context = context;
		final ViewConfiguration configuration = ViewConfiguration.get(this.context);
		touchSlop = configuration.getScaledTouchSlop();
		setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// Let the ScaleGestureDetector inspect all events.

		final int action = event.getAction();
		switch (action & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN: {
			final float x = event.getX();
			final float y = event.getY();
			mode = State.DRAG;
			mLastTouchX = x;
			mLastTouchY = y;
			mActivePointerId = event.getPointerId(0);
			break;
		}
		case MotionEvent.ACTION_POINTER_DOWN: {
			disLastTouch = MathUtil.getZoomDistance(event);
			if (disLastTouch > 10f) {
				mode = State.ZOOM;
			}
			break;
		}

		case MotionEvent.ACTION_MOVE: {
			final int pointerIndex = event.findPointerIndex(mActivePointerId);
			final float x = event.getX(pointerIndex);
			final float y = event.getY(pointerIndex);

			// Only move if the ScaleGestureDetector isn't processing a gesture.
			if (mode == State.DRAG) {
				final float dx = x - mLastTouchX;
				final float dy = y - mLastTouchY;
				mPosX += dx;
				mPosY += dy;
				updateView();
			} else if (mode == State.ZOOM) {
				float newZoomDistance = MathUtil.getZoomDistance(event);
				if (newZoomDistance > touchSlop) {
					float scale = newZoomDistance / disLastTouch;
					mScaleFactor = MathUtil.clamp(miniScaleFactor, scaleLastTouch * scale, maxScaleFactor);
					android.util.Log.d("temp", "new scale:" + scale + ", actual scale:" + mScaleFactor);
					updateView();
				}

			}
			// mLastTouchX = x;
			// mLastTouchY = y;

			break;
		}

		case MotionEvent.ACTION_UP: {
			mActivePointerId = INVALID_POINTER_ID;
			mode = State.NONE;
			break;
		}

		case MotionEvent.ACTION_CANCEL: {
			mActivePointerId = INVALID_POINTER_ID;
			mode = State.NONE;
			break;
		}

		case MotionEvent.ACTION_POINTER_UP: {
			mode = State.DRAG;
			final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
			final int pointerId = event.getPointerId(pointerIndex);
			if (pointerId == mActivePointerId) {
				// This was our active pointer going up. Choose a new
				// active pointer and adjust accordingly.
				final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
				mLastTouchX = event.getX(newPointerIndex);
				mLastTouchY = event.getY(newPointerIndex);
				scaleLastTouch = mScaleFactor;
				mActivePointerId = event.getPointerId(newPointerIndex);
			}
			break;
		}
		}

		return true;
	}

	public void updateView() {
		// android.util.Log.d("temp", "scale:" + mScaleFactor + ", dx:" + mPosX + ",dy:" + mPosY);
		if (mode == State.ZOOM) {
			this.setScaleX(mScaleFactor);
			this.setScaleY(mScaleFactor);
		} else {
			this.setTranslationX(mPosX);
			this.setTranslationY(mPosY);
		}

	}
	//
	// private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
	// @Override
	// public boolean onScale(ScaleGestureDetector detector) {

	// }
	// }
}
